package com.new_api;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class FilterDemo1 {
    private Configuration conf;
    private Connection conn;
    private Admin admin;

    @Before
    public void init() throws IOException {
        // 创建配置文件
        conf = HBaseConfiguration.create();
        // 加载zookeeper的配置
        conf.set("hbase.zookeeper.quorum", "node02,node03,node04");
        // 获取连接
        conn = ConnectionFactory.createConnection(conf);
        // 获取admin对象
        admin = conn.getAdmin();
    }

    /**
     * scan扫描多行数据
     */
    @Test
    public void filterTest() throws Exception {
        // 要先获取表
        TableName tableName = TableName.valueOf("api_test", "student");
        // 获取table对象
        Table table = conn.getTable(tableName);

        Scan scan = new Scan();

        // Filter filter = new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator("name".getBytes()));

        // ValueFilter 只会返回匹配到的列数据
        // Filter filter = new ValueFilter(CompareOperator.EQUAL, new BinaryComparator("女".getBytes()));

        DependentColumnFilter filter = new DependentColumnFilter(
                Bytes.toBytes("info"),
                Bytes.toBytes("name"),
                false,
                CompareOperator.EQUAL,
                new BinaryPrefixComparator(Bytes.toBytes("符半双")));

        scan.setFilter(filter);

        // 扫描一批数据
        ResultScanner scanner = table.getScanner(scan);

        printScanner(scanner);

        table.close();

    }

    @Test
    public void test1() throws Exception {
        // 要先获取表
        TableName tableName = TableName.valueOf("api_test", "student");
        // 获取table对象
        Table table = conn.getTable(tableName);

        // 不能全表扫描！！！！！
        Scan scan = new Scan();

//        RowFilter filter = new RowFilter(CompareOperator.NOT_EQUAL, new SubstringComparator("15001000"));

//        RowFilter filter = new RowFilter(CompareOperator.EQUAL, new BinaryPrefixComparator("150010001".getBytes()));

        // 三个维度，确定 过滤器、比较运算符、比较器
        FamilyFilter filter = new FamilyFilter(CompareOperator.LESS_OR_EQUAL, new BinaryComparator("info".getBytes()));

        scan.setFilter(filter);

        // 扫描一批数据
        ResultScanner scanner = table.getScanner(scan);

        printScanner(scanner);

        table.close();
    }

    // 打印扫描结果
    private void printScanner(ResultScanner scanner) {
        int count = 0;
        for (Result result : scanner) {
            String id = new String(result.getRow());
            String name = Bytes.toString(result.getValue("info".getBytes(), "name".getBytes()));
            String age = Bytes.toString(result.getValue("info".getBytes(), "age".getBytes()));
            String gender = Bytes.toString(result.getValue("info".getBytes(), "gender".getBytes()));
            String clazz = Bytes.toString(result.getValue("info".getBytes(), "clazz".getBytes()));
            String ct = Bytes.toString(result.getValue("info".getBytes(), "ct".getBytes()));
            System.out.println("id:" + id + ",name:" + name + ",age:" + age + ",gender:" + gender
                    + ",clazz:" + clazz + ",ct:" + ct);
            count++;
        }
        System.out.println("本次scan了 " + count + " 行数据！！！");
    }


    @After
    public void afterTest() throws IOException {
        admin.close();
        conn.close();
    }
}
